package com.ss.task.test;

import com.ss.task.dto.TaskUsers;
import com.ss.task.model.MemberEntity;
import com.ss.task.model.TaskEntity;
import com.ss.task.model.UserEntity;
import com.ss.task.service.TaskService;
import com.ss.task.service.UserService;
import com.ss.task.util.BaseController;
import com.ss.task.util.Util;
import com.ss.webutil.struct.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Tan Zhaowei.
 */
@RestController
@RequestMapping("test/newTask/")
public class NewTaskController extends BaseController {
    @Autowired
    UserService userService;

    @Autowired
    TaskService taskService;

    @RequestMapping("submit")
    public ResultModel<TaskEntity> submitTask(String str, HttpSession session){
        UserEntity user = getUser(session);
        TaskEntity task = new TaskEntity();
        TaskUsers tu = new TaskUsers();
        String start_time = "2010-08-09";
        String end_time = "2010-09-10";

        task.setStartDate(Util.parseDate(start_time));
        task.setEndDate(Util.parseDate(end_time));

        return taskService.addTask(user.getId(), task, tu.getTaskusers());
    }

    @RequestMapping("member")
    public ResultModel<List<MemberEntity>> getMember( HttpSession session){
        UserEntity user = getUser(session);
        ResultModel<List<UserEntity>> resultModel = userService.getUsersByLeader(user.getId());
        List<UserEntity> list = resultModel.getData();
        List<MemberEntity> memberEntityList = new ArrayList<MemberEntity>();
        for (int i=0;i<list.size();i++) {
            MemberEntity memberEntity = new MemberEntity();
            memberEntity.setId(list.get(i).getId());
            memberEntity.setName(list.get(i).getRealname());
            memberEntity.setType(list.get(i).getTitle());
            memberEntity.setDepartment(list.get(i).getDepartment().getTitle());
            memberEntityList.add(memberEntity);
        }
        return new ResultModel<List<MemberEntity>>(memberEntityList);
    }
}
